package com.lf2.tool;

import com.lf2.Lf_Debug;

import java.io.UnsupportedEncodingException;

public class FieldTypeUtil
{
    public FieldTypeUtil() {}

    public static String getDigitAmount(String value)
    {
        int p = value.indexOf('.');
        if (p == -1) {
            value = "00" + value;
            value = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
        }

        return String.format("%.2f", new Object[] { Double.valueOf(value) });
    }

    public static String getNumber(String value)
    {
        for (int i = 0; i < value.length(); i++) {
            if (value.charAt(i) != '0') {
                return value.substring(i);
            }
        }
        return value;
    }

    public static String increaseValue(String value)
    {
        int len = value.length();
        int v = Integer.parseInt(value);
        
        v++;
        String newValue = String.valueOf(v);

        StringBuilder str = new StringBuilder();
        for (int i = 0; i < len; i++) {
            str.append('0');
        }
        if (newValue.length() > value.length()) {
            return str.toString();
        }
        str.append(newValue);
        return str.substring(newValue.length());
    }

    public static void logBuffer(String tag, byte[] data)
    {
        if (data == null) {
            Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(),  "null");
            return;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < data.length; i++) {
            String str = "00" + Integer.toHexString(data[i] & 0xFF);
            builder.append(str.substring(str.length() - 2));
            builder.append(",");

            if (i % 16 == 15) {
                builder.append("\n");
            }
        }
        Lf_Debug.dbg_mPrintf(Lf_Debug._FILE_LINE_FUNC_(),  builder.toString());
    }

    public static String makeFieldAmount(String amount)
    {
        String[] part = amount.split("\\.");
        switch (part.length) {
        case 1: 
            return ("0000000000" + amount + "00").substring(amount.length());
        case 2: 
            String digit = part[1].length() >= 2 ? part[1].substring(0, 2) : (part[1] + "00").substring(0, 2);
            amount = "000000000000" + part[0] + digit;
            return amount.substring(amount.length() - 12);
        }
        
        return null;
    }

    public static String extend(String str)
    {
        return extend(str, "GBK");
    }

    public static String extend(String str, String charsetName)
    {
        try
        {
            byte[] gbk = str.getBytes(charsetName);
            return BytesUtil.byteArray2HexString(gbk);
        } 
        catch (UnsupportedEncodingException e) 
        {
            e.printStackTrace();
            Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return null;
    }

    public static String shrink(String str)
    {
        return shrink(str, "GBK");
    }

    public static String shrink(String str, String charsetName)
    {
        try
        {
            byte[] data = BytesUtil.hexString2ByteArray(str);
            return new String(data, charsetName);
        } 
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
            Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        } 
        catch (NullPointerException e) 
        {
            e.printStackTrace();
            Lf_Debug.e_printf(Lf_Debug._FILE_LINE_FUNC_(), e);
        }
        return null;
    }

    public static String shrinkRaw(String str)
    {
        byte[] data = BytesUtil.hexString2ByteArray(str);
        char[] chs = new char[data.length];
        for (int i = 0; i < chs.length; i++) {
            chs[i] = ((char)(data[i] & 0xFF));
        }
        return new String(chs);
    }

    public static String addPadding(String src, boolean isLeft, char padding, int fixLen)
    {
        if (src.length() >= fixLen) {
            return src;
        }

        StringBuilder b = new StringBuilder();
        int padLen = fixLen - src.length();
        for (int i = 0; i < padLen; i++) {
            b.append(padding);
        }
        if (isLeft) 
        {
            b.append(src);
        }
        else 
        {
            b.insert(0, src);
        }
        return b.toString();
    }
}
